import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollBar;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JLabel;
import javax.swing.JButton;


import java.awt.Font;


public class AccountManagerUI extends JPanel implements ActionListener{

	private AssetRegistryFrame arFrame;
	JComboBox cmbName = new JComboBox();
	JComboBox cmbUsertype = new JComboBox();
	private JLabel txtUsername;
	private JLabel txtFName;
	private JLabel txtLName;
	
	LoginUI login;
	String globalusername = login.GlobalUsername.getText();
	
	/**
	 * Create the panel.
	 */
	public AccountManagerUI(AssetRegistryFrame arFrame) {
		this.arFrame = arFrame;
		
		setLayout(null);
		
		cmbName.setBounds(236, 51, 200, 30);
		cmbName.addActionListener(this);
		add(cmbName);
		
		JLabel lblUsername = new JLabel("Username:");
		lblUsername.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblUsername.setBounds(56, 130, 67, 14);
		add(lblUsername);
		
		JLabel lblFirstName = new JLabel("First Name:");
		lblFirstName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFirstName.setBounds(352, 130, 89, 14);
		add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name:");
		lblLastName.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblLastName.setBounds(352, 209, 89, 14);
		add(lblLastName);
		
		JLabel lblUserType = new JLabel("User Type:");
		lblUserType.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblUserType.setBounds(56, 208, 67, 14);
		add(lblUserType);
		
		txtUsername = new JLabel();
		txtUsername.setBounds(56, 150, 200, 30);
		add(txtUsername);
		
		txtFName = new JLabel();
		txtFName.setBounds(352, 149, 165, 30);
		add(txtFName);
		
		txtLName = new JLabel();
		txtLName.setBounds(352, 234, 165, 30);
		add(txtLName);
		
		cmbUsertype.setBounds(56, 233, 200, 30);
		add(cmbUsertype);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSaveActionPerformed(e);
			}
		});
		btnSave.setBounds(260, 320, 89, 23);
		add(btnSave);
		
		JLabel lblSelectAccount = new JLabel("Select Account:");
		lblSelectAccount.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblSelectAccount.setBounds(123, 54, 115, 14);
		add(lblSelectAccount);
		
		setAccounts();
		
		//GET TYPES OF ACCOUNTS
		String query = "SELECT description FROM usertypes ORDER BY description";
		Connection con = DBConnector.getConnection();
		ResultSet rs;
		try {
		rs = DBConnector.executeQuery(con, query);
			while(rs.next()) {
				String list = rs.getString("description");
				cmbUsertype.addItem(list);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void setAccounts(){
		// GET CHOICES OF USERS
		String query = "SELECT concat(username, ' - ', lname, ', ', fname) AS user FROM useraccounts ORDER BY Lname";
		Connection con = DBConnector.getConnection();
		ResultSet rs;
		
		try {
			cmbName.removeAllItems();
			globalusername = login.GlobalUsername.getText().trim();
			rs = DBConnector.executeQuery(con, query);
			while(rs.next()) {
				String list = rs.getString("user");
				String username = list.replaceAll(" .*", "");
				if(username.compareTo(globalusername) != 0)
					cmbName.addItem(list);
			}
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void btnSaveActionPerformed(ActionEvent arg0) {
		String usertypedescription = (String)cmbUsertype.getSelectedItem();
		String usertypecode = getUsertypeCode(usertypedescription);
		String query = "UPDATE useraccounts SET usertype = '"+usertypecode+"' WHERE username = '"+txtUsername.getText()+"'";
		
		Connection con = DBConnector.getConnection();
		
		try {
			DBConnector.executeUpdate(con, query);
			DBConnector.alertUser("Change has been saved.");
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String getUsertypeDescription(String code){
		Connection con = DBConnector.getConnection();
		String query = "SELECT description FROM usertypes WHERE code = '"+code+"'";
		ResultSet rs= DBConnector.executeQuery(con, query);
		String description = null;
		
		try {
			while(rs.next())
				description = rs.getString("description");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return description;
	}
	
	public String getUsertypeCode(String description){
		Connection con = DBConnector.getConnection();
		String query = "SELECT code FROM usertypes WHERE description = '"+description+"'";
		ResultSet rs= DBConnector.executeQuery(con, query);
		String code = null;
		
		try {
			while(rs.next())
				code = rs.getString("code");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return code;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String DBusername = null;
		String DBfname = null;
		String DBlname = null;
		String DBusertype = null;
		
		if(arg0.getSource() == cmbName  && cmbName.getSelectedItem() != null) {
			Connection con = DBConnector.getConnection();
			String selectedItem = (String)cmbName.getSelectedItem();
			String username = selectedItem.replaceAll(" .*", "");
			String query = "SELECT username, lname, fname, usertype FROM useraccounts WHERE username = '"+username+"'";
			ResultSet rs= DBConnector.executeQuery(con, query);
			try {
				while(rs.next()){
					DBusername = rs.getString("username");
					DBfname = rs.getString("fname");
					DBlname = rs.getString("lname");
					DBusertype = rs.getString("usertype");
				}
				
				txtUsername.setText(DBusername);
				txtFName.setText(DBfname);
				txtLName.setText(DBlname);
				cmbUsertype.setSelectedItem(getUsertypeDescription(DBusertype));
				
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
